﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace StockLib.Quote
{
    public partial class StockReceiver
    {
        /// <summary>
        /// 收到行情数据
        /// </summary>
        /// <param name="tsdata">通视数据</param>
        private void ReceiveReport(ref ReceiveData tsdata)
        {
            // 保存行情数据
            SortedList<string, ReportData> reportData = new SortedList<string, ReportData>();
            for (int i = 0; i < tsdata.m_nPacketNum; i++)
            {
                ReceiveReportPack pack = (ReceiveReportPack)Marshal.PtrToStructure(new IntPtr((int)tsdata.m_pData + i * 158 - 2), typeof(ReceiveReportPack));
                if (!string.IsNullOrEmpty(pack.m_szLabel))
                {
                    ReportData data = new ReportData();
                    ConvertReportPackToInternal(ref pack, data);
                    reportData.Add(data.CodeKey, data);
                }
            }

            // 更新容器
            StockContainer.Instance.UpdateByReportData(reportData);

            // 通知行情数据接收
            //OnDataChanged(this, new ReceiverDataEventArgs(DataType.Report));
        }

        private void ConvertReportPackToInternal(ref ReceiveReportPack reportPack, ReportData reportData)
        {
            reportData.Market = ConvertMarketToInternal(reportPack.m_wMarket);
            reportData.Code = reportPack.m_szLabel;
            reportData.Name = reportPack.m_szName;
            reportData.Time = reportPack.m_time;
            reportData.Close = reportPack.m_fLastClose;
            reportData.Open = reportPack.m_fOpen;
            reportData.High = reportPack.m_fHigh;
            reportData.Low = reportPack.m_fLow;
            reportData.New = reportPack.m_fNewPrice;
            reportData.Volume = reportPack.m_fVolume;
            reportData.Amount = reportPack.m_fAmount;
            reportData.BuyPrice[0] = reportPack.m_fBuyPrice[0];
            reportData.BuyPrice[1] = reportPack.m_fBuyPrice[1];
            reportData.BuyPrice[2] = reportPack.m_fBuyPrice[2];
            reportData.BuyPrice[3] = reportPack.m_fBuyPrice4;
            reportData.BuyPrice[4] = reportPack.m_fBuyPrice5;
            reportData.BuyVolume[0] = reportPack.m_fBuyVolume[0];
            reportData.BuyVolume[1] = reportPack.m_fBuyVolume[1];
            reportData.BuyVolume[2] = reportPack.m_fBuyVolume[2];
            reportData.BuyVolume[3] = reportPack.m_fBuyVolume4;
            reportData.BuyVolume[4] = reportPack.m_fBuyVolume5;
            reportData.SellPrice[0] = reportPack.m_fSellPrice[0];
            reportData.SellPrice[1] = reportPack.m_fSellPrice[1];
            reportData.SellPrice[2] = reportPack.m_fSellPrice[2];
            reportData.SellPrice[3] = reportPack.m_fSellPrice4;
            reportData.SellPrice[4] = reportPack.m_fSellPrice5;
            reportData.SellVolume[0] = reportPack.m_fSellVolume[0];
            reportData.SellVolume[1] = reportPack.m_fSellVolume[1];
            reportData.SellVolume[2] = reportPack.m_fSellVolume[2];
            reportData.SellVolume[3] = reportPack.m_fSellVolume4;
            reportData.SellVolume[4] = reportPack.m_fSellVolume5;/**/
        }

    }
}
